﻿using UnityEngine;
using System.Collections;

public class CFS_Controller : Photon.MonoBehaviour 
{
	[SerializeField] Vector3 verticalVelocity = Vector3.zero;
	Vector3 velocity = Vector3.zero;
	
//	bool doubleJump = false;
	[SerializeField] float JumpForce = 50.0f;
	
//	public float JumpSpeed = 20;
	[SerializeField] float distToGround = 0.05f;
	
	[SerializeField] float Speed = 10.0f;
	
	[SerializeField] int GravityAccelLimit = 50;
	
	private Quaternion fromRotation;
	private Quaternion toRotation;
	
	[SerializeField] float rotationSpeed = 10.0f;
	
	public LayerMask l;
	
	void Awake()
	{
		if(!photonView.isMine)
		{
			enabled = false;
			Destroy(transform.FindChild("Main Camera").gameObject);
		}
		
		rigidbody.freezeRotation = true;
	}
	
	bool isGrounded()
	{
		Debug.DrawRay(transform.position, -transform.up * distToGround);
		return Physics.Raycast(transform.position, -transform.up, distToGround, l);
	}

	void Update () 
	{
		spherecast();
		SideRotation();
		CalculateMovements();
		UpdateVelocity();
		
		

	}
	
	void SideRotation()
	{
		float rotationX = Input.GetAxis("Mouse X");
		
		Vector3 newRotation = Vector3.up * rotationX;
		transform.Rotate(newRotation * rotationSpeed);
	}
	
	void CalculateMovements()
	{
		float y = Input.GetAxis("Vertical");
		float x = Input.GetAxis("Horizontal");
		
		if(!isGrounded())
		{
			verticalVelocity += CenterOfGravity.NewGravityDirection(transform.position);
			
			verticalVelocity = Vector3.ClampMagnitude(verticalVelocity, GravityAccelLimit);
		}
		else if(isGrounded() && !Input.GetButtonDown("Jump"))
		{
			verticalVelocity = Vector3.zero;
		}
		else if(Input.GetButtonDown("Jump") && isGrounded())
		{
			verticalVelocity = transform.up * JumpForce;
		}
		
		Vector3 directionForward = transform.forward.normalized * y;
		Vector3 directionSide = transform.right.normalized * x;
		
		velocity = (directionForward + directionSide) * Speed;
	}
	
	void UpdateVelocity()
	{
		Vector3 finalVelocity = velocity + verticalVelocity;
		
		rigidbody.velocity = finalVelocity * Timing.CustomDeltaTime();
	}
	
	void spherecast  ()
	{
		float distForward = Mathf.Infinity;
		RaycastHit hitForward;
		if (Physics.SphereCast(transform.position, 0.25f, -transform.up + transform.forward, out hitForward, 5, l))
		{
		    distForward = hitForward.distance;
		}
		float distDown = Mathf.Infinity;
		RaycastHit hitDown;
		if (Physics.SphereCast(transform.position, 0.25f, -transform.up, out hitDown, 5, l))
		{
		    distDown = hitDown.distance;
		}
		float distBack = Mathf.Infinity;
		RaycastHit hitBack;
		if (Physics.SphereCast(transform.position, 0.25f, -transform.up + -transform.forward, out hitBack, 5, l))
		{
		    distBack = hitBack.distance;
		}
		 
		if (distForward < distDown && distForward < distBack)
		{
		    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitForward.normal), hitForward.normal), Timing.CustomDeltaTime() * 5.0f);
		}
		else if (distDown < distForward && distDown < distBack)
		{
		    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitDown.normal), hitDown.normal), Timing.CustomDeltaTime() * 5.0f);
		}
		else if (distBack < distForward && distBack < distDown)
		{
		    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitBack.normal), hitBack.normal), Timing.CustomDeltaTime() * 5.0f);
		}
	}
	
	void OnCollisionEnter(Collision other) 
	{
      	if(other.collider.tag == "Bullet")
		{
			Destroy(other.gameObject);
		}
	}
}
